import { defineStore } from 'pinia'
import request from '@/utils/request'
import { IApiRes, Profile } from '@/types/data'
import { getProfile, removeProfile, setProfile } from '@/utils/profile'

export default defineStore('user', {
  state: () => {
    return {
      profile: getProfile() as Profile  // 用户信息
    }
  },
  actions: {
    // 密码登录
    async login(obj: {account: string, password: string}) {
      const res = await request.post<IApiRes<Profile>>('/login', obj)
      // console.log(res)
      this.profile = res.data.result
      // 持久化
      setProfile(res.data.result)
    },
    // 验证码登录
    async mobileLogin(mobile: string, code: string ) {
      const res = await request.post<IApiRes<Profile>>('/login/code', {mobile, code})
      console.log(res)
      this.profile = res.data.result
      // 持久化
      setProfile(res.data.result)
    },
    // 验证码请求
    async sendMobileMsg(mobile: string) {
      const res = await request.get('/login/code', {params: {mobile}})
      // console.log(res)
    },
    // 退出清空
    logout() {
      // 清空用户信息
      this.profile = {} as Profile
      // 清空持久化
      removeProfile()
    },
    // QQ登录验证
    async qqLogin(openId: string) {
      const res = await request.post('/login/social', {unionId: openId, source: 6})
      // console.log(res)

    },
    // 绑定qq的短信验证码
    async sendQQBindMsg(mobile: string) {
      await request.get('/login/social/code', {
        params: {
          mobile
        }
      })
    },
    // QQ绑定
    async qqBindLogin(openId: string, mobile: string, code: string) {
      const res = await request.post('/login/social/bind', {unionId: openId, mobile, code})
      console.log(res)
     
    }
  }
})